package com.higgins.server.agentapp.datamodel;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;

import com.higgins.client.agentapp.datamodel.AdvertisedJobDTO;
import com.higgins.client.dataModel.StrategyDTO;

@Entity
@Table(name = "advertisedjob")
public class AdvertisedJob {

    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
	private Long id;
    
    private String description; 
    private String jobCode; 
    
    @ManyToOne
    @JoinColumn(name="jobSiteId")
    private JobSite jobSite;

    @ManyToOne
    @JoinColumn(name="agencyId")
    private Agency agency;

    @ManyToOne
    @JoinColumn(name="agentId")
    private Agent agent;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="timestamp")
    private Date timestamp;
    
    @Column(name="status")
    private Integer status;

    public AdvertisedJob() {
        // This is used by JPA
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getJobCode() {
        return jobCode;
    }

    public void setJobCode(String jobCode) {
        this.jobCode = jobCode;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }
    
    public JobSite getJobSite()	{
    	return jobSite;
    }
    
    public void setJobSite(JobSite jobSite)	{
    	this.jobSite = jobSite;
    }
    public Agency getAgency()	{
    	return agency;
    }
    
    public void setAgency(Agency agency)	{
    	this.agency = agency;
    }
    public Agent getAgent()	{
    	return agent;
    }
    
    public void setAgent(Agent agent)	{
    	this.agent = agent;
    }
    public Integer getStatus()	{
    	return status;
    }
    public void setStatus(Integer status)	{
    	this.status = status;
    }
    public AdvertisedJobDTO toDTO()	{
    	AdvertisedJobDTO dto = new AdvertisedJobDTO();
    	dto.setId(id);
    	dto.setDescription(description);
    	dto.setJobCode(jobCode);
    	dto.setTimestamp(timestamp);
    	dto.setStatus(status);
    	if (jobSite!=null)	{
    		dto.setJobSiteDTO(jobSite.toDTO());
    	}
    	if (agency!=null)	{
    		dto.setAgencyDTO(agency.toDTO());
    	}
    	if (agent!=null)	{
    		dto.setAgentDTO(agent.toDTO());
    	}
    	return dto;
    }
    public void update(AdvertisedJobDTO dto)	{
    	this.description = dto.getDescription();
    	this.jobCode = dto.getJobCode();
    	this.timestamp = dto.getTimestamp();
    	this.status = dto.getStatus();
    	if (dto.getJobSiteDTO()!=null)	{
    		// Need to update the covering letter here!!
    	} else {
    		jobSite = null;
    	}
    	if (dto.getAgencyDTO()!=null)	{
    		// Need to update the covering letter here!!
    	} else {
    		agency = null;
    	}
    	if (dto.getAgentDTO()!=null)	{
    		// Need to update the covering letter here!!
    	} else {
    		agent = null;
    	}
    }

}